package com.example.music_liwei.fragment;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.example.music_liwei.R;
import com.example.music_liwei.bean.ResponseData;
import com.example.music_liwei.messageEvent.MessageEventCurrentIndexBoolean;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.lang.ref.WeakReference;
import java.util.List;

public class BlankFragmentGilde extends Fragment {

    private List<ResponseData.MusicInfo> musicInfoList;
    private ImageView iv_glide;
    private int currentIndex;
    private boolean isPlaying;
    private WeakReference<ImageView> iv_glideWeakReference;
    private ObjectAnimator objectAnimator;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View root = inflater.inflate(R.layout.fragment_blank_gilde, container, false);

        iv_glide = root.findViewById(R.id.iv_glide);

        // 初始化动画//////////////////////////////////////////////////////////////////////
        iv_glideWeakReference = new WeakReference<>(iv_glide);/////////////////
        objectAnimator = ObjectAnimator.ofFloat(iv_glideWeakReference.get(), "rotation", 0, 360);
        objectAnimator.setDuration(5000);
        objectAnimator.setRepeatCount(ValueAnimator.INFINITE);

        // 尝试从粘性事件中获取初始状态
        MessageEventCurrentIndexBoolean stickyEvent = EventBus.getDefault().getStickyEvent(MessageEventCurrentIndexBoolean.class);
        if (stickyEvent != null) {
            currentIndex = stickyEvent.getCurrentIndex();
            musicInfoList = stickyEvent.getMusicInfoList();
            isPlaying = stickyEvent.getIsPlay();
            updateUI();
            controlAnimation(isPlaying);
        }

        return root;
    }

    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEventCurrentIndexBoolean event) {
        currentIndex = event.getCurrentIndex();
        musicInfoList = event.getMusicInfoList();
        isPlaying = event.getIsPlay();
        updateUI();
        controlAnimation(isPlaying);
    }

//  updateUI 方法用于根据当前的 musicInfoList 更新封面图片 ///////////////////////////////////////////////////////////////
    private void updateUI() {
        if (musicInfoList != null && !musicInfoList.isEmpty()) {
            // 使用RequestOptions设置圆角属性
            RequestOptions options = new RequestOptions()
                    .transform(new CircleCrop());  // 使用CircleCrop进行圆形转换
            Glide.with(this)
                    .load(musicInfoList.get(currentIndex).getCoverUrl())
                    .apply(options)
                    .into(iv_glide);
        }
    }

//  方法用于根据 isPlaying 的状态控制封面图片的旋转动画。////////////////////////////////////////////////////////////////////////
    private void controlAnimation(boolean isPlaying) {
        if (isPlaying) {
            if (!objectAnimator.isRunning()) {
                objectAnimator.start();
            } else if (objectAnimator.isPaused()) {
                objectAnimator.resume();
            }
        } else {
            if (objectAnimator.isRunning()) {
                objectAnimator.pause();
            }
        }
    }

//  销毁EventBus和动画，放置内存泄漏，之前做过////////////////////////////////////////////////////////////////////////////
    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        objectAnimator.cancel();
    }
}
